<?php
class ModelToolSeoUrl extends Model {
	//--------Add function seourl-------------------------//
	//--------Convert from Vietnamese to Unsign Vietnamese//
	//--------July 17, 2012-------------------------------//
	//--------By Dinh Nguyen, Tan Phat--------------------//
	public function convert($str){
		$arrayTemp = array(
			'a'	=>	array('ấ','ầ','ẩ','ẫ','ậ','Ấ','Ầ','Ẩ','Ẫ','Ậ','ắ','ằ','ẳ','ẵ','ặ','Ắ','Ằ','Ẳ','Ẵ','Ặ','á','à','ả','ã','ạ','â','ă','Á','À','Ả','Ã','Ạ','Â','Ă','A'),
			'e' =>	array('ế','ề','ể','ễ','ệ','Ế','Ề','Ể','Ễ','Ệ','é','è','ẻ','ẽ','ẹ','ê','É','È','Ẻ','Ẽ','Ẹ','Ê','E'),
			'i'	=>	array('í','ì','ỉ','ĩ','ị','Í','Ì','Ỉ','Ĩ','Ị','I'),
			'o'	=>	array('ố','ồ','ổ','ỗ','ộ','Ố','Ồ','Ổ','Ô','Ộ','ớ','ờ','ở','ỡ','ợ','Ớ','Ờ','Ở','Ỡ','Ợ','ó','ò','ỏ','õ','ọ','ô','ơ','Ó','Ò','Ỏ','Õ','Ọ','Ô','Ơ','O'),
			'u'	=>	array('ứ','ừ','ử','ữ','ự','Ứ','Ừ','Ử','Ữ','Ự','ú','ù','ủ','ũ','ụ','ư','Ú','Ù','Ủ','Ũ','Ụ','Ư', 'U'),
			'y'	=>	array('ý','ỳ','ỷ','ỹ','ỵ','Ý','Ỳ','Ỷ','Ỹ','Ỵ','Y'),
			'd'	=>	array('đ','Đ','D'),
			'q' =>  array('Q'),
			'w' =>  array('W'),
			'r' =>  array('R'),
			't' =>  array('T'),
			'y' =>  array('Y'),
			'p' =>  array('P'),
			's' =>  array('S'),
			'f' =>  array('F'),
			'g' =>  array('G'),
			'h' =>  array('H'),
			'j' =>  array('J'),
			'k' =>  array('K'),
			'l' =>  array('L'),
			'z' =>  array('Z'),
			'x' =>  array('X'),
			'c' =>  array('C'),
			'v' =>  array('V'),
			'b' =>  array('B'),
			'n' =>  array('N'),
			'm' =>  array('M'),
			' ' =>  array(','),
			''  => 	array('[','-', ']', '+','(', ')','*', '&', '%','@','#','$','='),
			'-'	=>	array(' '),
			
			
		);
		foreach ($arrayTemp as $key => $arr)
			foreach ($arr as $val)
				$str = str_replace($val,$key,$str);
		return $str;
	}
	//---------End------------------------------------------//
	
	/*
	 * get list data
	 */
	public function get($id){
		//$sql = "SELECT url_alias_id, keyword FROM " . DB_PREFIX . "url_alias WHERE `url_alias_id` = '" .$id."'";
		//$sql = "SELECT url_alias_id, keyword FROM " . DB_PREFIX . "url_alias ";
		$sql = " select * from url_alias where query like '%category_id%'";
		$data = $this->db->query($sql);
		return $data->rows;
	}

	/*
	 * update url_alias
	 */
	public function update($id, $value){
		$sql = "update url_alias set keyword='" . $value . "' where url_alias_id = '" . $id . "'";
		$this->db->query($sql);
	}

	/*
	 * rewrite url
	 */
	public function rewrite($link) {
		if ($this->config->get('config_seo_url')) {
			$url_data = parse_url(str_replace('&amp;', '&', $link));
			$url = '';
			$data = array();
			parse_str($url_data['query'], $data);
			foreach ($data as $key => $value) {
				if (($key == 'product_id') || ($key == 'manufacturer_id') || ($key == 'information_id')) {
					$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
					if ($query->num_rows) {
						$url .= '/' . $query->row['keyword'];
						unset($data[$key]);
					}
				} elseif ($key == 'path') {
					$categories = explode('_', $value);
					foreach ($categories as $category) {
						$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");

						if ($query->num_rows) {
							$url .= '/' . $query->row['keyword'];
						}
					}
					unset($data[$key]);
				}else if($key == "ajax"){
					$url .= '/ajax';
				}
			}

			if ($url) {
				unset($data['route']);
				unset($data['ajax']);
				$query = '';
				if ($data) {
					foreach ($data as $key => $value) {
						$query .= '&' . $key . '=' . $value;
					}
					if ($query) {
						$query = '?' . str_replace('&amp;', '&', trim($query, '&'));
					}
				}
				return $url_data['scheme'] . '://' . $url_data['host'] . (isset($url_data['port']) ? ':' . $url_data['port'] : '') . str_replace('/index.php', '', $url_data['path']) . $url . $query;
			} else {
				return $link;
			}
		} 
		//--------Add code for SEO URL------------------------//
		//--------Replace $path by $name----------------------//
		//--------July 17, 2012-------------------------------//
		//--------By Dinh Nguyen, Tan Phat--------------------//
		else {
			$pos=strpos($link,"shops/shop_customer");
			if($pos!==false)
			{
				$pos=strpos($link, "product_id");
				if($pos!==false)
				{
					$substrName = substr($link, $pos + 11, strlen($link));
					$SQLTemp = " SELECT name FROM product WHERE product_id = $substrName";
					
					$queryTemp = $this->db->query($SQLTemp);
					$subStrName = $queryTemp->row['name'];
					$subStrName = $this->convert($subStrName);
					if(strlen($subStrName) > 50) 
						$subStrName = subStr($subStrName, 0, 50);
					$p1 = strpos($link, "path=");
					$p2 = strpos($link, "product_id=");
					$getHead = substr($link, 0, $p1);
					$getTail = substr($link, $p2, strlen($link));
					$link = "index.php?route=shops/shop_customer&" . "name=" . $subStrName . "&" . $getTail;
					return $link;
				}
			}
			return $link;
		}
		//-------End------------------------------------------//
	}
}
?>